package com.bp.hearthealth.pulse.lnapps.bp_view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.bp.hearthealth.pulse.lnapps.R

/**
 * @author isnoam
 * @package_name RoundedCornerImageView
 * @description：
 */
class BPRoundedCornerImageView : AppCompatImageView {
    private var cornerRadiusTopLeft = 0f
    private var cornerRadiusTopRight = 0f
    private var cornerRadiusBottomRight = 0f
    private var cornerRadiusBottomLeft = 0f
    private var uniformCornerRadius = 0f
    private var useCircular = false
    private var path: Path? = null

    constructor(context: Context) : super(context) {
        init(context, null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        path = Path()

        // 从 XML 获取自定义属性
        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.RoundedCornerImageView)
            cornerRadiusTopLeft =
                a.getDimension(R.styleable.RoundedCornerImageView_cornerRadiusTopLeft, 0f)
            cornerRadiusTopRight =
                a.getDimension(R.styleable.RoundedCornerImageView_cornerRadiusTopRight, 0f)
            cornerRadiusBottomRight =
                a.getDimension(R.styleable.RoundedCornerImageView_cornerRadiusBottomRight, 0f)
            cornerRadiusBottomLeft =
                a.getDimension(R.styleable.RoundedCornerImageView_cornerRadiusBottomLeft, 0f)
            uniformCornerRadius =
                a.getDimension(R.styleable.RoundedCornerImageView_uniformCornerRadius, 0f)
            useCircular = a.getBoolean(R.styleable.RoundedCornerImageView_useCircular, false)
            a.recycle()
        }
    }

    override fun onDraw(canvas: Canvas) {
        val width = width.toFloat()
        val height = height.toFloat()
        path!!.reset()
        if (useCircular) {
            // 使用圆形
            val radius = Math.min(width, height) / 2f
            path!!.addCircle(width / 2f, height / 2f, radius, Path.Direction.CW)
        } else {
            // 使用圆角
            if (uniformCornerRadius > 0) {
                path!!.addRoundRect(
                    0f,
                    0f,
                    width,
                    height,
                    uniformCornerRadius,
                    uniformCornerRadius,
                    Path.Direction.CW
                )
            } else {
                path!!.addRoundRect(
                    0f, 0f, width, height, floatArrayOf(
                        cornerRadiusTopLeft, cornerRadiusTopLeft,
                        cornerRadiusTopRight, cornerRadiusTopRight,
                        cornerRadiusBottomRight, cornerRadiusBottomRight,
                        cornerRadiusBottomLeft, cornerRadiusBottomLeft
                    ),
                    Path.Direction.CW
                )
            }
        }
        canvas.clipPath(path!!)
        super.onDraw(canvas)
    }
}
